package logic

import (
	"context"
	"fmt"
	"user/models/mysql"
	redisinit "user/models/redinit"

	"user/internal/svc"
	"user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserLoginLogic) UserLogin(in *user.UserLoginRequest) (*user.UserLoginResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{}
	mysql.Db.Debug().Where("mobile = ?", in.Mobile).Find(&u)
	if u.ID != 0 {
		return nil, fmt.Errorf("手机号错误")
	}
	get := redisinit.RedisClient.Get("sendSms" + "login" + in.Mobile)
	if get.Val() != in.Code {
		return nil, fmt.Errorf("短信验证码错误")
	}
	return &user.UserLoginResponse{
		Id: int64(u.ID),
	}, nil
}
